package firstShallowPersistencePackagev;

import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class PrPeProxy implements InvocationHandler, Serializable {
	private static final long serialVersionUID = 42L;
	private Integer id;
	private Broker b;

	public PrPeProxy(Broker br, Object o) {
		super();
		this.b = br;
		this.id = br.put(o);
	}

	public static Object newInstance(Broker br, Object o) {
		return Proxy.newProxyInstance(o.getClass().getClassLoader(), o
				.getClass().getInterfaces(), new PrPeProxy(br, o));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
	 * java.lang.reflect.Method, java.lang.Object[])
	 */
	public Object invoke(Object proxy, Method me, Object[] args)
			throws Throwable {
		Object result;
		result = null;
		try {
			result = b.runMethod(proxy, me, args, id);
		} catch (InvocationTargetException e) {
			javax.swing.JOptionPane.showMessageDialog(null,
					"Invocation Target Exception: " + e);
		}
		;
		return result;
	}
}